package com.imooc.ioc.demo1.Service;

import com.imooc.ioc.demo1.dao.UserMapper;
import com.imooc.ioc.demo1.vo.User;
import com.imooc.ioc.demo1.vo.Register;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper ;

    @Override
    public boolean selectUser(String username , Integer id) {

      if(userMapper.selectUser(username , id) != null) {
          return true ;
      }else {
          return false ;
      }
    }

    @Override
    public boolean select(User user) {

       Integer data = userMapper.select(user);


        if(data != null){

            return true;
        }

        return false ;


    }

    @Override
    public boolean insert(Register user) {

        com.imooc.ioc.demo1.vo.User user1 = new com.imooc.ioc.demo1.vo.User(user.getUsername(),user.getPassword());

        // 判断用户是否已经存在
        if(userMapper.select(user1) != null) {
            return  false ;
        }

        userMapper.insert(user);

        return true;
    }

    @Override
    public Register select(String username) {
         return userMapper.selectUserAll(username);
    }

    @Override
    public ArrayList<Register> getAllUser(Integer page , Integer limit) {
       return userMapper.getAllUser( (page - 1 )* limit ,  limit);
    }

    @Override
    public Integer getTotalUser() {
        return userMapper.count();
}

    @Override
    public Boolean updateUser(Register register) {

        userMapper.updateUser(register);

        return true ;
    }

    @Override
    public Boolean delete(Integer id) {
        userMapper.delete(id);
        return true ;
    }
}
